package com.tinem.platform.service.network.client.impl;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import com.tinem.platform.module.starter.jpa.entity.network.IpInfo;
import com.tinem.platform.module.starter.jpa.entity.network.IpRangeInfo;
import com.tinem.platform.module.starter.jpa.entity.network.Ipv6RangeInfo;
import com.tinem.platform.module.starter.jpa.repository.network.IpRangeInfoRepository;
import com.tinem.platform.module.starter.jpa.repository.network.Ipv6RangeInfoRepository;
import com.tinem.platform.service.network.client.IpInfoClient;
//import io.netty.util.NetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Optional;

/**
 * @program: platform
 * @description: ${description}
 * @author: eden#can
 * @Slogan: 没有所谓失败，除非你不再尝试
 * @date: 2022-05-10 16:41
 **/
@Slf4j
@Service
@Configuration
public class IpRangeInfoClient implements IpInfoClient {

    @Resource
    Ipv6RangeInfoRepository ipv6RangeInfoRepository;

    @Resource
    IpRangeInfoRepository ipRangeInfoRepository;


    @Override
    public IpInfo getLocationByIp(String ip) {

        IpInfo ipInfo = new IpInfo();
        ipInfo.setIp(ip);


        if(io.netty.util.NetUtil.isValidIpV4Address(ip)){
            long l = NetUtil.ipv4ToLong(ip);
            Optional<IpRangeInfo> ipv4InfoByIpNum = ipRangeInfoRepository.findIpv4InfoByIpNum(l);
            if (!ipv4InfoByIpNum.isPresent()) {
                return null;
            }
            IpRangeInfo ipRangeInfo = ipv4InfoByIpNum.get();
            ipInfo.setIp(ip);
            if (StrUtil.isBlank(ipRangeInfo.getCountry())) {
                // 国家为空，则忽略该查询
                return null;
            }
            ipInfo.setCountry(ipRangeInfo.getCountry());
            ipInfo.setProvince(ipRangeInfo.getProvince());
            ipInfo.setCity(ipRangeInfo.getCity());
            ipInfo.setIsp(ipRangeInfo.getLocation());
        } else {
            String ipv6 = this.ipv6To16Integer(ip);
            Optional<Ipv6RangeInfo> ipv6InfoByIpRange = ipv6RangeInfoRepository.findIpv6InfoByIpRange(ipv6);
            if (!ipv6InfoByIpRange.isPresent()) {
                return null;
            }
            Ipv6RangeInfo ipv6RangeInfo = ipv6InfoByIpRange.get();
            ipInfo.setIp(ip);
            if (StrUtil.isBlank(ipv6RangeInfo.getCountry())) {
                // 国家为空，则忽略该查询
                return null;
            }
            ipInfo.setCountry(ipv6RangeInfo.getCountry());
            ipInfo.setProvince(ipv6RangeInfo.getProvince());
            ipInfo.setCity(ipv6RangeInfo.getCity());
            ipInfo.setIsp(ipv6RangeInfo.getLocation());
        }
        return ipInfo;
    }


    private String ipv6To16Integer(String ip) {
        StringBuffer ipv6 = new StringBuffer();
        String[] split = ip.split(":");

        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            ipv6.append(String.format("%4s", s).replace(" ","0"));
        }
        return ipv6.toString();
    }


}
